import axios from '../js_sdk/xtshadow-axios/axios.min'
import {
	baseURL
} from '../config'

const service = axios.create({
	baseURL: baseURL,
	timeout: 10000,
	header: {
		'content-type': 'application/json'
	},
})

service.interceptors.request.use(
	config => {
		const {
			params = {}
		} = config
		return config
	},
	error => {
		uni.showToast({
			title: error.message || '系统开小差',
			icon: "none"
		})
		console.log(error) // for debug
		return Promise.reject(error)
	}
)

service.interceptors.response.use(
	async (response) => {
			const {
				code,
				message
			} = response.data
			if (code !== 200) {
				uni.showToast({
					title: message || '系统开小差',
					icon: "none"
				})
				if (code === 30000) {
					store.commit('LOGOUT')
					toLogin()
				}
				return Promise.reject(response.data)
			}
			return response.data
		},
		error => {
			uni.showToast({
				title: error.message || '系统开小差',
				icon: "none"
			})
			return Promise.reject(error)
		}
)

export default service
